1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.DBusUtilities;
26 
27 private import gio.AsyncResultIF;
28 private import gio.Cancellable;
29 private import gio.IOStream;
30 private import gio.c.functions;
31 public  import gio.c.types;
32 private import glib.ErrorG;
33 private import glib.GException;
34 private import glib.MemorySlice;
35 private import glib.Str;
36 private import glib.Variant;
37 private import glib.VariantType;
38 private import glib.c.functions;
39 private import gobject.ObjectG;
40 private import gobject.Value;
41 
42 
43 /** */
44 public struct DBusUtilities
45 {
46 
47 	/**
48 	 * Escape @string so it can appear in a D-Bus address as the value
49 	 * part of a key-value pair.
50 	 *
51 	 * For instance, if @string is `/run/bus-for-:0`,
52 	 * this function would return `/run/bus-for-%3A0`,
53 	 * which could be used in a D-Bus address like
54 	 * `unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0`.
55 	 *
56 	 * Params:
57 	 *     string_ = an unescaped string to be included in a D-Bus address
58 	 *         as the value in a key-value pair
59 	 *
60 	 * Returns: a copy of @string with all
61 	 *     non-optionally-escaped bytes escaped
62 	 *
63 	 * Since: 2.36
64 	 */
65 	public static string addressEscapeValue(string string_)
66 	{
67 		auto retStr = g_dbus_address_escape_value(Str.toStringz(string_));
68 
69 		scope(exit) Str.freeString(retStr);
70 		return Str.toString(retStr);
71 	}
72 
73 	/**
74 	 * Synchronously looks up the D-Bus address for the well-known message
75 	 * bus instance specified by @bus_type. This may involve using various
76 	 * platform specific mechanisms.
77 	 *
78 	 * The returned address will be in the
79 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
80 	 *
81 	 * Params:
82 	 *     busType = a #GBusType
83 	 *     cancellable = a #GCancellable or %NULL
84 	 *
85 	 * Returns: a valid D-Bus address string for @bus_type or
86 	 *     %NULL if @error is set
87 	 *
88 	 * Since: 2.26
89 	 *
90 	 * Throws: GException on failure.
91 	 */
92 	public static string addressGetForBusSync(GBusType busType, Cancellable cancellable)
93 	{
94 		GError* err = null;
95 
96 		auto retStr = g_dbus_address_get_for_bus_sync(busType, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
97 
98 		if (err !is null)
99 		{
100 			throw new GException( new ErrorG(err) );
101 		}
102 
103 		scope(exit) Str.freeString(retStr);
104 		return Str.toString(retStr);
105 	}
106 
107 	/**
108 	 * Asynchronously connects to an endpoint specified by @address and
109 	 * sets up the connection so it is in a state to run the client-side
110 	 * of the D-Bus authentication conversation. @address must be in the
111 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
112 	 *
113 	 * When the operation is finished, @callback will be invoked. You can
114 	 * then call g_dbus_address_get_stream_finish() to get the result of
115 	 * the operation.
116 	 *
117 	 * This is an asynchronous failable function. See
118 	 * g_dbus_address_get_stream_sync() for the synchronous version.
119 	 *
120 	 * Params:
121 	 *     address = A valid D-Bus address.
122 	 *     cancellable = A #GCancellable or %NULL.
123 	 *     callback = A #GAsyncReadyCallback to call when the request is satisfied.
124 	 *     userData = Data to pass to @callback.
125 	 *
126 	 * Since: 2.26
127 	 */
128 	public static void addressGetStream(string address, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
129 	{
130 		g_dbus_address_get_stream(Str.toStringz(address), (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
131 	}
132 
133 	/**
134 	 * Finishes an operation started with g_dbus_address_get_stream().
135 	 *
136 	 * A server is not required to set a GUID, so @out_guid may be set to %NULL
137 	 * even on success.
138 	 *
139 	 * Params:
140 	 *     res = A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
141 	 *     outGuid = %NULL or return location to store the GUID extracted from @address, if any.
142 	 *
143 	 * Returns: A #GIOStream or %NULL if @error is set.
144 	 *
145 	 * Since: 2.26
146 	 *
147 	 * Throws: GException on failure.
148 	 */
149 	public static IOStream addressGetStreamFinish(AsyncResultIF res, out string outGuid)
150 	{
151 		char* outoutGuid = null;
152 		GError* err = null;
153 
154 		auto __p = g_dbus_address_get_stream_finish((res is null) ? null : res.getAsyncResultStruct(), &outoutGuid, &err);
155 
156 		if (err !is null)
157 		{
158 			throw new GException( new ErrorG(err) );
159 		}
160 
161 		outGuid = Str.toString(outoutGuid);
162 
163 		if(__p is null)
164 		{
165 			return null;
166 		}
167 
168 		return ObjectG.getDObject!(IOStream)(cast(GIOStream*) __p, true);
169 	}
170 
171 	/**
172 	 * Synchronously connects to an endpoint specified by @address and
173 	 * sets up the connection so it is in a state to run the client-side
174 	 * of the D-Bus authentication conversation. @address must be in the
175 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
176 	 *
177 	 * A server is not required to set a GUID, so @out_guid may be set to %NULL
178 	 * even on success.
179 	 *
180 	 * This is a synchronous failable function. See
181 	 * g_dbus_address_get_stream() for the asynchronous version.
182 	 *
183 	 * Params:
184 	 *     address = A valid D-Bus address.
185 	 *     outGuid = %NULL or return location to store the GUID extracted from @address, if any.
186 	 *     cancellable = A #GCancellable or %NULL.
187 	 *
188 	 * Returns: A #GIOStream or %NULL if @error is set.
189 	 *
190 	 * Since: 2.26
191 	 *
192 	 * Throws: GException on failure.
193 	 */
194 	public static IOStream addressGetStreamSync(string address, out string outGuid, Cancellable cancellable)
195 	{
196 		char* outoutGuid = null;
197 		GError* err = null;
198 
199 		auto __p = g_dbus_address_get_stream_sync(Str.toStringz(address), &outoutGuid, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
200 
201 		if (err !is null)
202 		{
203 			throw new GException( new ErrorG(err) );
204 		}
205 
206 		outGuid = Str.toString(outoutGuid);
207 
208 		if(__p is null)
209 		{
210 			return null;
211 		}
212 
213 		return ObjectG.getDObject!(IOStream)(cast(GIOStream*) __p, true);
214 	}
215 
216 	/**
217 	 * Generate a D-Bus GUID that can be used with
218 	 * e.g. g_dbus_connection_new().
219 	 *
220 	 * See the
221 	 * [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#uuids)
222 	 * regarding what strings are valid D-Bus GUIDs. The specification refers to
223 	 * these as ‘UUIDs’ whereas GLib (for historical reasons) refers to them as
224 	 * ‘GUIDs’. The terms are interchangeable.
225 	 *
226 	 * Note that D-Bus GUIDs do not follow
227 	 * [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
228 	 *
229 	 * Returns: A valid D-Bus GUID. Free with g_free().
230 	 *
231 	 * Since: 2.26
232 	 */
233 	public static string generateGuid()
234 	{
235 		auto retStr = g_dbus_generate_guid();
236 
237 		scope(exit) Str.freeString(retStr);
238 		return Str.toString(retStr);
239 	}
240 
241 	/**
242 	 * Converts a #GValue to a #GVariant of the type indicated by the @type
243 	 * parameter.
244 	 *
245 	 * The conversion is using the following rules:
246 	 *
247 	 * - `G_TYPE_STRING`: 's', 'o', 'g' or 'ay'
248 	 * - `G_TYPE_STRV`: 'as', 'ao' or 'aay'
249 	 * - `G_TYPE_BOOLEAN`: 'b'
250 	 * - `G_TYPE_UCHAR`: 'y'
251 	 * - `G_TYPE_INT`: 'i', 'n'
252 	 * - `G_TYPE_UINT`: 'u', 'q'
253 	 * - `G_TYPE_INT64`: 'x'
254 	 * - `G_TYPE_UINT64`: 't'
255 	 * - `G_TYPE_DOUBLE`: 'd'
256 	 * - `G_TYPE_VARIANT`: Any #GVariantType
257 	 *
258 	 * This can fail if e.g. @gvalue is of type %G_TYPE_STRING and @type
259 	 * is 'i', i.e. %G_VARIANT_TYPE_INT32. It will also fail for any #GType
260 	 * (including e.g. %G_TYPE_OBJECT and %G_TYPE_BOXED derived-types) not
261 	 * in the table above.
262 	 *
263 	 * Note that if @gvalue is of type %G_TYPE_VARIANT and its value is
264 	 * %NULL, the empty #GVariant instance (never %NULL) for @type is
265 	 * returned (e.g. 0 for scalar types, the empty string for string types,
266 	 * '/' for object path types, the empty array for any array type and so on).
267 	 *
268 	 * See the g_dbus_gvariant_to_gvalue() function for how to convert a
269 	 * #GVariant to a #GValue.
270 	 *
271 	 * Params:
272 	 *     gvalue = A #GValue to convert to a #GVariant
273 	 *     type = A #GVariantType
274 	 *
275 	 * Returns: A #GVariant (never floating) of
276 	 *     #GVariantType @type holding the data from @gvalue or an empty #GVariant
277 	 *     in case of failure. Free with g_variant_unref().
278 	 *
279 	 * Since: 2.30
280 	 */
281 	public static Variant gvalueToGvariant(Value gvalue, VariantType type)
282 	{
283 		auto __p = g_dbus_gvalue_to_gvariant((gvalue is null) ? null : gvalue.getValueStruct(), (type is null) ? null : type.getVariantTypeStruct());
284 
285 		if(__p is null)
286 		{
287 			return null;
288 		}
289 
290 		return new Variant(cast(GVariant*) __p, true);
291 	}
292 
293 	/**
294 	 * Converts a #GVariant to a #GValue. If @value is floating, it is consumed.
295 	 *
296 	 * The rules specified in the g_dbus_gvalue_to_gvariant() function are
297 	 * used - this function is essentially its reverse form. So, a #GVariant
298 	 * containing any basic or string array type will be converted to a #GValue
299 	 * containing a basic value or string array. Any other #GVariant (handle,
300 	 * variant, tuple, dict entry) will be converted to a #GValue containing that
301 	 * #GVariant.
302 	 *
303 	 * The conversion never fails - a valid #GValue is always returned in
304 	 * @out_gvalue.
305 	 *
306 	 * Params:
307 	 *     value = A #GVariant.
308 	 *     outGvalue = Return location pointing to a zero-filled (uninitialized) #GValue.
309 	 *
310 	 * Since: 2.30
311 	 */
312 	public static void gvariantToGvalue(Variant value, out Value outGvalue)
313 	{
314 		GValue* outoutGvalue = sliceNew!GValue();
315 
316 		g_dbus_gvariant_to_gvalue((value is null) ? null : value.getVariantStruct(), outoutGvalue);
317 
318 		outGvalue = ObjectG.getDObject!(Value)(outoutGvalue, true);
319 	}
320 
321 	/**
322 	 * Checks if @string is a
323 	 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
324 	 *
325 	 * This doesn't check if @string is actually supported by #GDBusServer
326 	 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
327 	 * checks.
328 	 *
329 	 * Params:
330 	 *     string_ = A string.
331 	 *
332 	 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
333 	 *
334 	 * Since: 2.26
335 	 */
336 	public static bool isAddress(string string_)
337 	{
338 		return g_dbus_is_address(Str.toStringz(string_)) != 0;
339 	}
340 
341 	/**
342 	 * Checks if @string is a D-Bus GUID.
343 	 *
344 	 * See the documentation for g_dbus_generate_guid() for more information about
345 	 * the format of a GUID.
346 	 *
347 	 * Params:
348 	 *     string_ = The string to check.
349 	 *
350 	 * Returns: %TRUE if @string is a GUID, %FALSE otherwise.
351 	 *
352 	 * Since: 2.26
353 	 */
354 	public static bool isGuid(string string_)
355 	{
356 		return g_dbus_is_guid(Str.toStringz(string_)) != 0;
357 	}
358 
359 	/**
360 	 * Checks if @string is a valid D-Bus interface name.
361 	 *
362 	 * Params:
363 	 *     string_ = The string to check.
364 	 *
365 	 * Returns: %TRUE if valid, %FALSE otherwise.
366 	 *
367 	 * Since: 2.26
368 	 */
369 	public static bool isInterfaceName(string string_)
370 	{
371 		return g_dbus_is_interface_name(Str.toStringz(string_)) != 0;
372 	}
373 
374 	/**
375 	 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
376 	 *
377 	 * Params:
378 	 *     string_ = The string to check.
379 	 *
380 	 * Returns: %TRUE if valid, %FALSE otherwise.
381 	 *
382 	 * Since: 2.26
383 	 */
384 	public static bool isMemberName(string string_)
385 	{
386 		return g_dbus_is_member_name(Str.toStringz(string_)) != 0;
387 	}
388 
389 	/**
390 	 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
391 	 *
392 	 * Params:
393 	 *     string_ = The string to check.
394 	 *
395 	 * Returns: %TRUE if valid, %FALSE otherwise.
396 	 *
397 	 * Since: 2.26
398 	 */
399 	public static bool isName(string string_)
400 	{
401 		return g_dbus_is_name(Str.toStringz(string_)) != 0;
402 	}
403 
404 	/**
405 	 * Like g_dbus_is_address() but also checks if the library supports the
406 	 * transports in @string and that key/value pairs for each transport
407 	 * are valid. See the specification of the
408 	 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
409 	 *
410 	 * Params:
411 	 *     string_ = A string.
412 	 *
413 	 * Returns: %TRUE if @string is a valid D-Bus address that is
414 	 *     supported by this library, %FALSE if @error is set.
415 	 *
416 	 * Since: 2.26
417 	 *
418 	 * Throws: GException on failure.
419 	 */
420 	public static bool isSupportedAddress(string string_)
421 	{
422 		GError* err = null;
423 
424 		auto __p = g_dbus_is_supported_address(Str.toStringz(string_), &err) != 0;
425 
426 		if (err !is null)
427 		{
428 			throw new GException( new ErrorG(err) );
429 		}
430 
431 		return __p;
432 	}
433 
434 	/**
435 	 * Checks if @string is a valid D-Bus unique bus name.
436 	 *
437 	 * Params:
438 	 *     string_ = The string to check.
439 	 *
440 	 * Returns: %TRUE if valid, %FALSE otherwise.
441 	 *
442 	 * Since: 2.26
443 	 */
444 	public static bool isUniqueName(string string_)
445 	{
446 		return g_dbus_is_unique_name(Str.toStringz(string_)) != 0;
447 	}
448 }